<template>
	<view>
		<!-- <button type="default" @click="handleLogin">授权登录</button> -->
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				code: null
			}
		},
		onLoad() {
			this.handleLogin()

		},
		computed: {
			...mapState(['userInfo', 'hasLogin'])
		},
		methods: {
			...mapMutations(['setUserInfo', 'setLogin']),
			showModal(str, url) {
				uni.showModal({
					title: '提示',
					content: str,
					showCancel: false,
					success: function(res) {
						if (res.confirm) {
							uni.switchTab({
								url: url
							})
						}
					}
				});
			},
			showModalToNav(str, url) {
				uni.showModal({
					title: '提示',
					content: str,
					success: function(res) {
						if (res.confirm) {
							uni.navigateTo({
								url: url
							})
						}else if(res.cancel) {
							uni.switchTab({
								url: '../index/index'
							})
						}
					}
				});
			},
			handleLogin(e) {
				uni.showLoading({
					title: '登陆中'
				})
				uni.login({
					provider: 'weixin',
					success: res => {
						console.log(res.code)
						if (res.code) {
							//发起网络请求
							// this.code = res.code

							this.$http2.post('auth/wx/code2login', {
								code: res.code,
								authType: 1,
								groupId: this.$config.groupId
							}).then(res => {
								console.log(res)
								if (res.code === 50004) {
									uni.hideLoading()
									uni.setStorageSync('authkey', res.data.authkey)
									uni.setStorageSync('authType', res.data.authType)
									this.showModalToNav('用户不存在,请进行手机号验证', '../phone/phone')
									// uni.showModal({
									// 	title: '提示',
									// 	content: '用户不存在,请进行手机号验证',
									// 	showCancel: false,
									// 	success: function(res) {
									// 		if (res.confirm) {
									// 			uni.navigateTo({
									// 				url: '../phone/phone'
									// 			})
									// 		} 
									// 	}
									// });
									// this.showModal('用户不存在,请进行手机号验证','../phone/phone')
								} else if (res.code === 10000) {
									uni.hideLoading()
									// uni.showToast({
									// 	title: '登录成功'
									// })
									this.getUserInfo()
									this.getProjectList()
								}
							})
						} else {
							console.log('登录失败！' + res.errMsg)
						}
					}
				})
			},
			// 获取用户信息
			getUserInfo() {
				this.$http.post('common/user/info',{
						projId: uni.getStorageSync('projId') || null,
						groupId: this.$config.groupId
					}).then(res => {
					if (res.code === 10000 || res.code === 50014 || res.code === 50015) {
						uni.setStorageSync('projId', res.data.projId)
						uni.setStorageSync('userInfo', res.data)
					}

					if (res.code === 10000) {
						// console.log(res.data.checkStatus)

						uni.showToast({
							title: '登录成功'
						})
						setTimeout(() => {
							uni.switchTab({
								url: '../index/index'
							})
						}, 1000)
					} else if (res.code === 50013) {
						this.showModalToNav('您还没有注册,请前往注册', '../register/register')
					} else if (res.code === 50014) {

						this.showModal('您的个人信息处于待审核状态,部分功能将不可用,请尽快前往小区物业进行审核', '../index/index')
						// this.showModal('您的个人信息处于待审核状态,部分功能将不可用,请尽快前往小区物业进行审核','../index/index')
					} else if (res.code === 50015) {


						this.showModalToNav('您的个人信息审核不通过,请重新完善个人信息', '../userinfo/userinfo')
					}
				})
			},
			getProjectList() {
				let params = {
					pagination: {
						current: 1,
						pageSize: 1000
					},
					sorter: {
						field: "id",
						order: 0
					},
					queryType: 36
					// projType: this.$config.projType,
					// guId: this.$config.groupId
				}
				this.$http2.post('/sys/project/list', params).then(res => {
					if (res.code === 10000) {
						//项目列表设置到storage
						uni.setStorageSync('projList', res.data.list)
					}
				})
			},
		}
	}
</script>

<style>

</style>
